package com.yihubang.taobaokotlin.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import butterknife.ButterKnife
import butterknife.Unbinder
import com.yihubang.taobaokotlin.R

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/5/10.
 * PS: Not easy to write code, please indicate.
 */
abstract class BaseFragment : Fragment() {

    private lateinit var bind: Unbinder

    enum class State {
        NOME, LOADING, SUCCESS, ERROR, EMPTY
    }

    private lateinit var baseContainer: FrameLayout

    private lateinit var successView: View
    private lateinit var loadingView: View
    private lateinit var errorView: View
    private lateinit var emptyView: View

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val rootView = loadRootView(inflater, container)
        baseContainer = rootView.findViewById(R.id.base_container)
        loadSatesView(inflater, container)
        bind = ButterKnife.bind(this, rootView)
        initView(rootView)
        initListener()
        initPresenter()
        return rootView
    }

    open fun initPresenter() {

    }

    open fun initListener() {

    }

    abstract fun initView(rootView: View)

    private fun loadRootView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.base_fragment_layout, container, false)
    }

    /**
     * 加载各种状态的View
     */
    private fun loadSatesView(inflater: LayoutInflater, container: ViewGroup?) {
        //成功的View
        successView = loadSuccessView(inflater, container)
        baseContainer.addView(successView)
        //Loading的View
        loadingView = loadLoadingView(inflater, container)
        baseContainer.addView(loadingView)
        //错误页面
        errorView = loadErrorView(inflater, container)
        baseContainer.addView(errorView)
        //内容为空页面
        emptyView = loadEmptyView(inflater, container)
        baseContainer.addView(emptyView)
        //默认全部设置为不可见
        setUpState(State.NOME)
    }

    fun setUpState(status: State) {
        successView.visibility = if (status == State.SUCCESS) View.VISIBLE else View.GONE
        loadingView.visibility = if (status == State.LOADING) View.VISIBLE else View.GONE
        errorView.visibility = if (status == State.ERROR) View.VISIBLE else View.GONE
        emptyView.visibility = if (status == State.EMPTY) View.VISIBLE else View.GONE
    }

    //内容加载成功页面
    private fun loadSuccessView(inflater: LayoutInflater, container: ViewGroup?): View {
        val resId = getRootViewResId()
        return inflater.inflate(resId, container, false)
    }

    //正在加载中的页面
    private fun loadLoadingView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_loading, container, false)
    }

    //加载错误的页面
    private fun loadErrorView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_error, container, false)
    }

    //内容为空的页面
    private fun loadEmptyView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_empty, container, false)
    }

    //返回的是布局文件的id
    abstract fun getRootViewResId(): Int

    override fun onDestroy() {
        super.onDestroy()
        release()
    }

    open fun release() {
        //释放资源
    }
}